using System;
using System.Collections.Generic;
using System.Reflection;
using Function.SkillEditor.Runtime;

namespace Function.SkillEditor.Editor
{
    public class SkillTrackDrawerHelper
    {
        public static List<TrackRegisterAttribute> FindAllTrackRegisters()
        {
            List<TrackRegisterAttribute> trackedClasses = new List<TrackRegisterAttribute>();

            // 获取当前应用程序域中所有程序集
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var assembly in assemblies)
            {
                // 获取程序集中定义的所有类型
                Type[] types = assembly.GetTypes();
                foreach (var type in types)
                {
                    // 检查类型是否被 TrackRegister 特性标记
                    if (Attribute.IsDefined(type, typeof(TrackRegisterAttribute)))
                    {
                        var attribute =
                            Attribute.GetCustomAttribute(type,
                                typeof(TrackRegisterAttribute)) as TrackRegisterAttribute;
                        attribute._trackType = type;
                        trackedClasses.Add(attribute);
                    }
                }
            }


            return trackedClasses;
        }

        public static List<TrackDrawEditorAttribute> FindAllDrawEditors()
        {
            List<TrackDrawEditorAttribute> trackedClasses = new List<TrackDrawEditorAttribute>();

            // 获取当前应用程序域中所有程序集
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var assembly in assemblies)
            {
                // 获取程序集中定义的所有类型
                Type[] types = assembly.GetTypes();
                foreach (var type in types)
                {
                    // 检查类型是否被 TrackRegister 特性标记
                    if (Attribute.IsDefined(type, typeof(TrackDrawEditorAttribute)))
                    {
                        var attribute =
                            Attribute.GetCustomAttribute(type,
                                typeof(TrackDrawEditorAttribute)) as TrackDrawEditorAttribute;
                        attribute._drawType = type;
                        trackedClasses.Add(attribute);
                    }
                }
            }


            return trackedClasses;
        }
    }
}